package com.android.topview.watermelon.network

import com.android.topview.watermelon.exchange.ExchangeCode
import org.json.JSONObject

object NetworkGetter {

    private val service = RetrofitManager.getRetrofit()
        .create(RetrofitModel::class.java)

    internal fun getExchangeRate(
        from: ExchangeCode,
        to: ExchangeCode
    ): Double {
        if (from == to) return 1.0
        val model = service.exchangeRate(from.code, to.code)
        val data = model.execute().body()!!.string()
        val json = JSONObject(data)
        val msg = json.getString("msg")
        if (msg != "success") return -2.0
        return json.getJSONObject("data").getDouble("rate")
    }

}